	var map;
	var ChinaMap;
	var Airport;
	var AirLine = null;
	var EndPoint = null;
	var vector;
	var source;
	var draw, snap;
	var CurX;
	var CurY;
	var PosX = 0.0;
	var PosY = 0.0;
	var RunFlag = false;
	var index = 0;
    var StepX ;
	var StepY ;
	var TempX;
	var TempY ;
	var DirX;
	var DirY;
	var marker;
	var Tips;
	var popup; 
	var PupupDiv;
	var wether = new Object;
	var windDescribe;
	var windLevel;
	var temperature;
	var DemValue;
	var TemDemValue = 0;
	function load() {
		var bounds = [ 73.2516174316406, 17.9822978973389, 135.080017089844,
				53.7378730773926 ];
		// 		var mousePositionControl = new ol.control.MousePosition({
		// 			className : 'custom-mouse-position',
		// 			target : document.getElementById('location'),
		// 			coordinateFormat : ol.coordinate.createStringXY(2),
		// 			undefinedHTML : '&nbsp;'
		// 		});
		ChinaMap = new ol.layer.Tile({
			source : new ol.source.OSM()
		});
//		 		ChinaMap = new ol.layer.Tile({
//		 			visible : true,setVisible
//		 			source : new ol.source.TileWMS({
//		 				url : 'http://10.70.94.96:8080/geoserver/GisTest/wms',
//		 				params : {
//		 					'FORMAT' : 'image/png',
//		 					'VERSION' : '1.1.1',
//		 					tiled : true,
//		 					style : '',
//		 					LAYERS : 'GisTest:chinadem',
//		 				//tilesOrigin : 73.2516174316406 + "," + 17.9822978973389
//		 				}
//		 			})
//		 		});
//		var projection = ol.proj.get('CGCS2000');
//		  var projectionExtent = projection.getExtent();
//		  var size = ol.extent.getWidth(projectionExtent) / 256;
//		  var resolutions = new Array(14);
//		  var matrixIds = new Array(14);
//		  for (var z = 0; z < 14; ++z) {
//		    // generate resolutions and matrixIds arrays for this WMTS
//		    resolutions[z] = size / Math.pow(2, z);
//		    matrixIds[z] = z;
//		  }
// 		ChinaMap = new ol.layer.Tile({
//		 			visible : true,
//		 			source : new ol.source.WMTS({
//		 				url : 'http://t0.tianditu.com/img_w/wmts',
//		 				layer: 'img',
//		 				style:'default',
//		 				matrixSet: 'CGCS2000',
//		 				projection: projection,
//		 				tileGrid: new ol.tilegrid.WMTS({
//		 	                origin: ol.extent.getTopLeft(projectionExtent),
//		 	                resolutions: resolutions,
//		 	                matrixIds: matrixIds
//		 	              })
//		 			})
//		 		});
		// 		Airport = new ol.layer.Tile({
		// 			visible : true,
		// 			source : new ol.source.TileWMS({
		// 				url : 'http://10.70.94.96:8080/geoserver/GisTest/wms',
		// 				params : {
		// 					'FORMAT' : 'image/png',
		// 					'VERSION' : '1.1.1',
		// 					tiled : true,
		// 					STYLES : '',
		// 					LAYERS : 'GisTest:airportinfo',
		// 				//tilesOrigin : 73.2516174316406 + "," + 17.9822978973389
		// 				}
		// 			})
		// 		});

 		EVI = new ol.layer.Tile({
 			visible : true,
 			source : new ol.source.TileWMS({
 				url : 'http://10.70.94.96:8080/geoserver/GisTest/wms',
 				params : {
 					'FORMAT' : 'image/png',
 					'VERSION' : '1.1.1',
 					tiled : true,
 					STYLES : '',
 					LAYERS : 'GisTest:MODEV1D.20160517.CN.EVI.V2',
 				//tilesOrigin : 73.2516174316406 + "," + 17.9822978973389
 				}
 			})
 		});

 		NDVI = new ol.layer.Tile({
 			visible : true,
 			source : new ol.source.TileWMS({
 				url : 'http://10.70.94.96:8080/geoserver/GisTest/wms',
 				params : {
 					'FORMAT' : 'image/png',
 					'VERSION' : '1.1.1',
 					tiled : true,
 					STYLES : '',
 					LAYERS : 'GisTest:MODND1D.20160516.CN.NDVI.V2',
 				//tilesOrigin : 73.2516174316406 + "," + 17.9822978973389
 				}
 			})
 		});
 		
 		NDVI.setVisible(false);
//		var raster =new ol.layer.Image({
//            source:  new ol.source.Raster({
//                sources: [new ol.source.TileWMS({
//                	
//                	url : 'http://10.70.94.96:8080/geoserver/GisTest/wms',
//                	LAYERS: 'GisTest:MODEV1D.20160517.CN.EVI.V2',
//                	'FORMAT' : 'image/png',
//            		'VERSION' : '1.1.1',
//                  })],
//                  operation: function flood(pixels, data) {
//                      var pixel = pixels[0];
//                      if (pixel[3]) {
//                        var height = -10000 + ((pixel[0] * 256 * 256 + pixel[1] * 256 + pixel[2]) * 0.1);
//                        if (height <= data.level) {
//                          pixel[0] = 145;
//                          pixel[1] = 175;
//                          pixel[2] = 186;
//                          pixel[3] = 255;
//                        } else {
//                          pixel[3] = 0;
//                        }
//                      }
//                      return pixel;
//                    }
//            })
//        });
		var Src = new ol.source.Vector();
		vector = new ol.layer.Vector({
			source : Src,
			style : new ol.style.Style({
				image : new ol.style.Icon(({
					size : [ 48, 72 ],
					anchor : [ 0.5, 0.75 ],
					anchorXUnits : 'fraction',
					anchorYUnits : 'fraction',
					src : '/statics/img/marker.png'
				}))
			})
		});

		var projection = new ol.proj.Projection({
			code : 'EPSG:4326',
			units : 'degrees',
			axisOrientation : 'neu',
			global : false
		});
		map = new ol.Map({
			// 			controls : ol.control.defaults({
			// 				attribution : false,
			// 				ScaleLine: true
			// 			}).extend([ mousePositionControl ]),
			controls : ol.control.defaults({attribution : false}),
			target : 'map',
			loadTilesWhileAnimating: true,
			layers : [ ChinaMap , EVI, NDVI, vector],
			view : new ol.View({
				projection : projection,
				zoom : 6,
				maxZoom: 18
			})
		});
//		Src.on('addfeature', function(e) {
//			flashFeatures(e.feature);
//	      })
		//	map.getView().fit(bounds, map.getSize());
		SetCurPos();
		map.getView().setCenter([CurX, CurY]);
		marker = new ol.Overlay({
//          position: new ol.proj.fromLonLat([CurX, CurY]),
//          positioning: 'center-center',
            element: document.getElementById('Marker'),
//          stopEvent: false
        });
		
        map.addOverlay(marker);
        
        popup = new ol.Overlay({
            element: document.getElementById('popup')
        });
        map.addOverlay(popup);
//        map.on('singleclick', ClickEvn) ;
        
//        PupupDiv = document.createElement('div');
//        PupupDiv.innerText = "hahhah";
//        PupupDiv.className = "PupupDiv";
        var container = document.getElementById('popup');
        var content = document.getElementById('popup-content');
        var closer = document.getElementById('popup-closer');
  
        closer.onclick = function() {
        	popup.setPosition(undefined);
            closer.blur();
            return false;
          };
          
          map.on('singleclick', function(evt) {
              var coordinate = evt.coordinate;
              //var wether = "晴天";
              GetWetherInfo(coordinate[0], coordinate[1]);
              
              content.innerHTML ='<p>风向:' + windDescribe + '</p><p>风力:' + windLevel + '</p><p>气温:' + temperature + '</p>';
              
              popup.setPosition(coordinate);
              
          }); 
	};
	function checkOsm(elem){
		AirLine.setVisible(false);
	};
	function GetWetherInfo(PosX, PosY){
		$.ajax({
			url : "http://localhost/NerStation",
			type : "GET",
			data : {"PosX":PosX, "PosY":PosY},
			async: false,
//			contentType : 'text/plain; charset=UTF-8',
			dataType : "json",
			success : function(data) {				
				error = data.code ; 
				if(error === "10000"){
					windDescribe = data.result.data.windDescribe;
					windLevel = data.result.data.windLevel;
					temperature = data.result.data.temperature;
				}else{
					windDescribe = "unknow";
					windLevel = "unknow";
					temperature = "unknow";
				}
//				alert(windDescribe);
			//	wether = data.result.data;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.status);
				alert(XMLHttpRequest.readyState);
				alert(textStatus);
			}
		});
	}
//	var ClickEvn = function(event){
//		var element = popup.getElement();
//        var coordinate = event.coordinate;
////        alert(coordinate[0]);
//    //    $(element).innerHTML= '<p>The location you clicked was:</p><code>' + coordinate[0] + '</code>';
//        var wether = "晴天";
//        PupupDiv.innerHTML = '<p>天气:' + wether + '</p><p>位置:' + coordinate[0].toFixed(2) + ',' + coordinate[1].toFixed(2) + '</p>';
////        $(element).popover({
////            'placement': 'top',
////            'animation': false,
////            'html': true,
////            'content': '<p>The location you clicked was:</p><code>' + coordinate[0] + '</code>'
////          });
////          $(element).popover('show');
//        popup.setElement(PupupDiv);
//        popup.setPosition(coordinate);
//	};
	function RefreshFun() {
		var CurrentZoom = map.getView().getZoom();
		alert(CurrentZoom);
		AirLine.getSource().refresh({
			force : true,
			active : true
		});
		map.getView().setZoom(CurrentZoom);
	};

	function AddTarget(Posx, PosY, ImgPath) {
		if(ImgPath === ""){			
			ImgPath = "/statics/img/marker.png";
//			if(startPoint !== null){
//				vector.getSource().removeFeature(startPoint);
//			}
		}
		//	startPoint = new ol.Feature(new ol.geom.Point([Posx, PosY]));  
		startPoint = new ol.Feature(new ol.geom.Point([Posx, PosY]));
		FeatStyle = new ol.style.Style({
			image : new ol.style.Icon(({
				size : [ 24, 36 ],
				anchor : [ 0.5, 0.75 ],
				anchorXUnits : 'fraction',
				anchorYUnits : 'fraction',
				src : ImgPath
			}))
		});
		startPoint.setStyle(FeatStyle);
		vector.getSource().addFeature(startPoint);

//		map.removeLayer(vector);
//		vector.getSource().refresh({
//			force : true,
//			active : true
//		});
//		map.addLayer(vector);
	};


	function getLocation() {
		if (navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(showPosition, showError);
		} else {
			alert("Geolocation is not supported by this browser.");
		}
	};
	function showPosition(position) {
//		alert("Latitude: " + position.coords.latitude
//				+ "<br />Longitude: " + position.coords.longitude);
		CurX = position.coords.latitude;
		CurY = position.coords.longitude;
	};
	function showError(error) {
		switch (error.code) {
		case error.PERMISSION_DENIED:
			alert("User denied the request for Geolocation.");
			break;
		case error.POSITION_UNAVAILABLE:
			alert("Location information is unavailable.");
			break;
		case error.TIMEOUT:
			alert("The request to get user location timed out.");
			break;
		case error.UNKNOWN_ERROR:
			alert("An unknown error occurred.");
			break;
		}
	};
	
	
	
    function GetAirportInfo(){
    	//var AirportName = getElementbyid();
    	var AirportName = document.getElementById("TargetAirport");

    	$.ajax({
			url : "http://localhost/AirportPos",
			type : "GET",
			async: false,
			data : {"AirportName":AirportName.value},
//			contentType : 'text/plain; charset=UTF-8',
			dataType : "json",
			success : function(data) {				
				PosX = data.PosX ; 
				PosY = data.PosY;
				DrawLine();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.status);
				alert(XMLHttpRequest.readyState);
				alert(textStatus);
			}
		});
    	
    	
    };
    
    function SetCurPos(){
//    	getLocation();
//    	if(CurX == "")
//    		CurX = 104.3;
//    	if(CurY == "")
//    		CurY = 38.2;
    	CurY = 38.2;
    	CurX = 104.3;
    	AddTarget(CurX, CurY, "/statics/img/marker-blue.png");
    	
    };
    function keyLogin(){
    	 if (event.keyCode==13)  //回车键的键值为13
    	   document.getElementById("SearchButton").click(); 
    };
    function DrawLine(){
    	var StartPos = [CurX, CurY];
    	var EndPos = [PosX, PosY];
    	
    	var EndFeatStyle;
    	var Center = [CurX + (PosX - CurX)/2, CurY + (PosY - CurY)/2];
    	if(AirLine !== null){
    		vector.getSource().removeFeature(AirLine);
    		vector.getSource().removeFeature(EndPoint);
    	}
    	AirLine = new ol.Feature(new ol.geom.LineString([StartPos, EndPos]));
    	AirLine.setStyle(new ol.style.Style({  
            stroke: new ol.style.Stroke({  
                    width: 3,  
                   color: [237, 212, 0, 0.8] 
                  })  
            }));
		vector.getSource().addFeature(AirLine);
		
		map.getView().setCenter(Center);
		map.getView().setZoom(5);
		var wgs84Sphere = new ol.Sphere(6378137); 
		var Distance = wgs84Sphere.haversineDistance(StartPos, EndPos)/1000; 
//		alert(Distance);
		
		//	startPoint = new ol.Feature(new ol.geom.Point([Posx, PosY]));  
		EndPoint = new ol.Feature(new ol.geom.Point([PosX, PosY]));
		EndFeatStyle = new ol.style.Style({
			image : new ol.style.Icon(({
				size : [ 24, 36 ],
				anchor : [ 0.5, 0.75 ],
				anchorXUnits : 'fraction',
				anchorYUnits : 'fraction',
				src : '/statics/img/marker.png'
			})),
			text: new ol.style.Text({
				font: '18px 微软雅黑' ,
			    text: '距离:' + Distance.toFixed(0) + '公里',
			    offsetY:'30'
			})
		});
		EndPoint.setStyle(EndFeatStyle);
		vector.getSource().addFeature(EndPoint);
    };
    function GetDemInfo(PosX, PosY){
    	$.ajax({
			url : "http://localhost/DEMInfo",
			type : "GET",
			async: false,
			data : {"PosX":PosX, "PosY":PosY},
//			contentType : 'text/plain; charset=UTF-8',
			dataType : "json",
			success : function(data) {				
				DemValue = data;
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.status);
				alert(XMLHttpRequest.readyState);
				alert(textStatus);
			}
		});
    	
    	
    };
    var MoveFeature = function(event) {
        var vectorContext = event.vectorContext;
        var frameState = event.frameState;
        TempX+= StepX;
        TempY+= StepY;
        if(RunFlag){
        	
        	var currentPoint = new ol.geom.Point([TempX, TempY]);
        	if(DirX ===1  && TempX >= PosX){
        		RunFlag = false;
        		map.un('postcompose', MoveFeature);
        		Tips.style.visibility="hidden"; 
        		return;
        	}else if(DirX === -1 && TempX >= CurX){
        		RunFlag = false;
        		map.un('postcompose', MoveFeature);
        		Tips.style.visibility="hidden"; 
        		return;
        	}
        		
        	//alert(TempX);
        	var wgs84Sphere = new ol.Sphere(6378137);        	
        	var CurDist = wgs84Sphere.haversineDistance([TempX, TempY], [PosX, PosY])/1000; 
        	
        	/*每100公里获取一次高度数据*/
        	if(TemDemValue === 0 || TemDemValue - CurDist >= 100){
        		TemDemValue = CurDist;
        		GetDemInfo(TempX, TempY);
        	}
        	var feature = new ol.Feature(currentPoint);
            vectorContext.drawFeature(feature, new ol.style.Style(
            		{image :
            			new ol.style.Circle({
            	            radius: 7,
            	            snapToPixel: false,
            	            fill: new ol.style.Fill({color: 'black'}),
            	            stroke: new ol.style.Stroke({
            	              color: 'white', width: 2
            	            })
            	          })		
            		}
    				
            ));
           
            Tips.innerHTML = "<p>高度:" + DemValue.toFixed(0) + "米</p><p>距离:" +CurDist.toFixed(0) + "</p>";
            //marker.setElement(Tips);
            marker.setPosition([TempX, TempY]);
            map.render();
        }
        
    }
    function StartFly(){
    	var Target = document.getElementById("TargetAirport").value;
    	if(Target === ""){
    		alert("请输入目的地");
    		return;
    	}
    	RunFlag = true;
//    	var route = AirLine.getGeometry().getCoordinates();
//    	var routes = route.getCoordinates();
    	if(PosX > CurX){
    		DirX = 1;
    	}else{
    		DirX = -1;
    	}
    	if(PosY > CurY){
    		DirY = 1;
    	}else{
    		DirY = -1;
    	}
    	StepX = (Math.abs(PosX - CurX)/500)*DirX;
    	StepY = (Math.abs(PosY - CurY)/500)*DirY;
    	TempX = CurX;
    	TempY = CurY;
 
//    	marker.setPosition([CurX, CurY]);
//        marker.setElement("\<P\>名称：测试\</P\>");
    	Tips = document.getElementById('Marker');
    	Tips.style.visibility="visible"; 
    	map.on('postcompose', MoveFeature);
        map.render();
    };
    
    function StopFly(){
    	
    };
    
   
    